package com.gmstu.springorm.framework;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author John.zhang
 * @version Id: QueryRule, v 1.0 2020/9/1 15:11 ZhiYong Exp $
 */
public class QueryRule implements Serializable {

    public static final int ASC_ORDER = 101;
    public static final int DESC_ORDER = 102;
    public static final int LIKE = 1;
    public static final int EQ = 5;
    public static final int IN = 2;

    public static final int AND = 201;
    public static final int OR = 202;

    private List<Rule> ruleList = new ArrayList<Rule>();
    private List<QueryRule> queryRuleList = new ArrayList<QueryRule>();
    private String propertyName;

    public QueryRule() {
    }

    public QueryRule(String propertyName) {
        this.propertyName = propertyName;
    }

    public static QueryRule getInstance() {
        return new QueryRule();
    }


    public QueryRule addAscOrder(String propertyName) {
        this.ruleList.add(new Rule(ASC_ORDER, propertyName));
        return this;
    }

    public QueryRule addDescOrder(String propertyName) {
        this.ruleList.add(new Rule(DESC_ORDER, propertyName));
        return this;
    }

    public QueryRule andLike(String propertyName, Object value) {
        this.ruleList.add(new Rule(LIKE, propertyName, new Object[]{value}).setAndOr(AND));
        return this;
    }

    public QueryRule andEqual(String propertyName, Object value) {
        this.ruleList.add(new Rule(EQ, propertyName, new Object[]{value}).setAndOr(AND));
        return this;
    }

    public QueryRule andIn(String propertyName, Object... values) {
        this.ruleList.add(new Rule(IN, propertyName, values).setAndOr(AND));
        return this;
    }

    public QueryRule orLike(String propertyName, Object value) {
        this.ruleList.add(new Rule(LIKE, propertyName, new Object[]{value}).setAndOr(OR));
        return this;
    }


    public List<Rule> getRuleList() {
        return ruleList;
    }

    protected class Rule {
        private int type;

        private String propertyName;

        private Object[] values;

        private int andOr = AND;


        public Rule(int type, String propertyName) {
            this.type = type;
            this.propertyName = propertyName;
        }

        public Rule(int type, String propertyName, Object[] values) {
            this.type = type;
            this.propertyName = propertyName;
            this.values = values;
        }

        public int getType() {
            return type;
        }

        public String getPropertyName() {
            return propertyName;
        }

        public Object[] getValues() {
            return values;
        }

        public Rule setAndOr(int andOr) {
            this.andOr = andOr;
            return this;
        }

        public int getAndOr() {
            return andOr;
        }
    }
}
